package com.soup.view;

import com.soup.dao.PersonDao;
import com.soup.domain.Person;
import com.soup.utils.StringUtils;
import com.soup.view.component.MassageTip;
import com.soup.view.component.MeFrame;
import com.soup.view.value.ColorValue;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Register extends MeFrame implements MouseListener, DocumentListener {

    private JLabel title = new JLabel("用户注册");

    private JTextField id = new JTextField(10);
    private JTextField name = new JTextField(10);
    private JPasswordField pass = new JPasswordField(10);
    private JPasswordField againPass = new JPasswordField(10);
    private JButton reg = new JButton("注册");

    private PersonDao dao = new PersonDao();

    public Register() {
        super("注册",600, 450);

        title.setBounds(30,40,350,50);
        context.add(title,28);
        title.setForeground(Color.RED);

        id.setBounds(120,120,350,48);
        context.add(id,"学号",100);
        id.getDocument().addDocumentListener(this);

        name.setBounds(120,170,350,48);
        context.add(name,"姓名",100);

        pass.setEchoChar('*');
        pass.setBounds(120,220,350,48);
        context.add(pass,"密码",100);

        againPass.setEchoChar('*');
        againPass.setBounds(120,270,350,48);
        context.add(againPass,"确认",100);

        reg.setBounds(120,350,350,42);
        context.add(reg,20);

        reg.addActionListener(this);
    }

    @Override
    public void doActionPerformed(ActionEvent e) {

        if (e.getSource() == reg){

            if (!StringUtils.isEmpty(id.getText()) && id.getText().length() == 9){
                if (!StringUtils.isEmpty(name.getText())){

                    if (new String(pass.getPassword()).equals(new String(againPass.getPassword()))){
                        Person person = new Person();
                        person.setId(id.getText());
                        person.setName(name.getText());
                        person.setPassword(new String(pass.getPassword()));
                        person.setInd("学生");
                        person.setState("正常");
                        person.setMoney(200);
                        int result = dao.addPerson(person);
                        System.out.println(result);
                        if (result > 0){
                            MassageTip.showMassage(this,"注册成功");
                        }else{
                            MassageTip.showMassage(this,"注册失败");
                        }
                    }else{
                        MassageTip.showMassage(this,"两次密码不相同");
                    }

                }else{
                    MassageTip.showMassage(this,"名字不能为空");
                }

            }else {
                MassageTip.showMassage(this,"学号不正确");
            }

        }

    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        if (e.getSource() == reg){
            reg.setForeground(Color.RED);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        if (e.getSource() == reg){
            reg.setForeground(Color.LIGHT_GRAY);
        }
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        if (e.getDocument() == id.getDocument()){
            if (id.getText().length() != 9){
                id.setForeground(Color.RED);
            }else{
                id.setForeground(ColorValue.ContextForeground);
            }
        }
    }

    @Override
    public void removeUpdate(DocumentEvent e) {

    }

    @Override
    public void changedUpdate(DocumentEvent e) {

    }
}
